package flow

import (
	"context"
	"encoding/json"
	"ez/apps/flow/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type Flowing struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64        `json:"id" bson:"id,omitempty"`
	Title       string       `json:"title" bson:"title"`           //主题
	TargetId    int64        `json:"targetId" bson:"targetId"`     //目标ID
	TargetType  string       `json:"targetType" bson:"targetType"` //目标类型
	Flow        *Flow        `json:"flow" bson:"flow"`             //审批流程
	FlowId      int64        `json:"flowId" bson:"flowId"`         //审批流程
	Index       int64        `json:"index" bson:"index"`           //当前审批节点
	FlowCur     *FLowPoint   `json:"flowCur" bson:"flowCur"`       //当前审批节点
	Flows       []*FLowPoint `json:"flows" bson:"flows"`           //审批过程
	State       string       `json:"state" bson:"state"`           //状态
	Owner       int64        `json:"owner" bson:"owner"`           //所有者
	IsConfirm   bool         `json:"isConfirm" bson:"isConfirm"`   //是否生效
	ConfirmAt   time.Time    `json:"confirmAt" bson:"confirmAt"`   //生效时间
	IsCancel    bool         `json:"isCancel" bson:"isCancel"`     //是否取消
	CreateAt    time.Time    `json:"createAt" bson:"createAt"`
	UpdateAt    time.Time    `json:"updateAt" bson:"updateAt"`
	DeleteAt    *time.Time   `json:"deleteAt" bson:"deleteAt"`
}

func (this *Flowing) DocName() string { return "Flowing" }
func (this *Flowing) GetId() int64    { return this.Id }
func (this *Flowing) SetId(id int64)  { this.Id = id }
func (this *Flowing) Create() error {
	return this.GetFactory().Create(this)
}
func (this *Flowing) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *Flowing) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *Flowing) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *Flowing) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *Flowing) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *Flowing) ToString() string {
	return string(this.ToBytes())
}
func (this *Flowing) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Flowing) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Flowing) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *Flowing) LoadFlow() {
	if this.FlowId == 0 {
		return
	}
	this.Flow, _ = NewFlowCrud().FindId(this.FlowId)
}
func (this *Flowing) ClearRelationsBeforeSave() mgo.Doc {
	this.Flow = nil
	return this
}
func neverUsed_Flowing() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type FlowingAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *Flowing
	Session *ez.Session
}

func NewFlowingAccessControl(model *Flowing, action string, session *ez.Session) *FlowingAccessControl {
	ctrl := &FlowingAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.FlowingAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
